package com.wnxy.carl.carlorderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.carl.carlorderassembly.entity.ShoppingCart;
import com.wnxy.carl.carlorderassembly.mapper.ShoppingCartMapper;
import com.wnxy.carl.carlorderassembly.service.IShoppingCartService;
import com.wnxy.carl.dto.PageDTO;
import com.wnxy.carl.dto.ShoppingDTO;
import com.wnxy.carl.exception.DemoRuntimeException;
import com.wnxy.carl.param.IncreaseShopingParam;
import com.wnxy.carl.param.PageParam;
import com.wnxy.carl.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    // 查看用户购物车 PageDTO<ShoppingDTO>
    public PageDTO<ShoppingDTO> seeShoppingCart(PageParam<Integer> pageParam) {
        log.info("开始执行查看用户购物车功能：参数={}",pageParam);
        IPage<ShoppingCart> page = new Page<>(pageParam.getPageIndex(),pageParam.getPageSize());
        IPage<ShoppingCart> shoppingCartS = shoppingCartMapper.selectPage(page, new QueryWrapper<ShoppingCart>().eq("user_id", pageParam.getData()));
        PageDTO<ShoppingDTO> shoppingDTOPageDTO = PageDTO.bulid((Page<?>) shoppingCartS, ShoppingDTO.class);
        return shoppingDTOPageDTO;
    }
    // 修改购物车信息 ShoppingDTO
    public ShoppingDTO amendShoppingCart(Integer shoppingCartId, Integer number) {
        log.info("开始执行修改购物车信息功能：参数={},{}",shoppingCartId,number);
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(shoppingCartId);
        if (null == shoppingCart){
            throw new DemoRuntimeException("没有该商品");
        }
        UpdateWrapper<ShoppingCart> uw = new UpdateWrapper<>();
        uw.set("number",number);
        uw.set("goods_total",number*shoppingCart.getGoodsPrice());
        int update = shoppingCartMapper.update(null, uw);
        if (update == 0){
            throw new DemoRuntimeException("修改失败");
        }
        shoppingCart.setNumber(number);
        ShoppingDTO shoppingDTO = new ShoppingDTO();
        BeanUtils.copyProperties(shoppingCart,shoppingDTO);
        return shoppingDTO;
    }
    // 删除购物商品 Boolean
    public void cancelShoping(Integer shoppingCartId) {
        log.info("删除购物商品功能：参数={}",shoppingCartId);
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(shoppingCartId);
        if (null == shoppingCart){
            throw new DemoRuntimeException("没有该商品");
        }
        int i = shoppingCartMapper.deleteById(shoppingCartId);
        if (i == 0){
            throw new DemoRuntimeException("删除失败");
        }
    }
    // 添加购物车商品 ShoppingDTO
    public ShoppingDTO increaseShoping(IncreaseShopingParam shopingParam) {
        log.info("添加购物车商品功能：参数={}",shopingParam);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shopingParam,shoppingCart);
        shoppingCart.setGoodsTotal(shopingParam.getNumber()*shopingParam.getGoodsPrice());
        int insert = shoppingCartMapper.insert(shoppingCart);
        if (insert == 0 ){
            throw new DemoRuntimeException("添加失败");
        }
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectById(shoppingCart.getShoppingCartId());
        ShoppingDTO shoppingDTO = new ShoppingDTO();
        BeanUtils.copyProperties(shoppingCart,shoppingDTO);
        return shoppingDTO;
    }

}
